package org.zoomdev.zoom.dao.impl;

import java.util.Arrays;

public class EntityKey {

    String[] tables;
    Class<?> type;

    int h;

    public EntityKey(Class<?> type, String... tables) {
        this.type = type;
        for (int i = 0; i < tables.length; ++i) {
            tables[i] = tables[i].toLowerCase();
        }
        this.tables = tables;
    }

    @Override
    public int hashCode() {
        int h = this.h;
        if (h == 0) {
            if (type != null) {
                h = 31 + type.hashCode();
            }
            for (String table : tables) {
                h = 31 * h + table.hashCode();
            }
            this.h = h;
        }
        return h;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof EntityKey) {
            EntityKey key = (EntityKey) obj;
            if (key.type != this.type) {
                return false;
            }

            return Arrays.equals(key.tables, this.tables);

        }
        return false;
    }
}
